package cn.iocoder.yudao.module.api.shop.info.dto;

import cn.iocoder.yudao.framework.business.address.pojo.vo.PrimaryAddressVO;
import cn.iocoder.yudao.framework.business.basic.enums.EnumCodable;
import cn.iocoder.yudao.framework.business.basic.pojo.bo.BusinessHours;
import cn.iocoder.yudao.framework.business.evaluation.enums.ScoreLevelEnum;
import cn.iocoder.yudao.module.api.shop.category.eums.ShopCategoryCodeEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nonnull;
import lombok.Data;

import java.time.LocalTime;
import java.util.Collections;
import java.util.List;

/**
 * 店铺信息 Response DTO
 *
 * @author 山野羡民（1032694760@qq.com）
 */
@Data
public class ShopInfoDTO extends PrimaryAddressVO implements BusinessHours {

    public static final ShopInfoDTO FALLBACK = new ShopInfoDTO();

    @Schema(description = "用户编号（前台用户），消费者和商家聊天时要用到", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long userId;

    @Schema(description = "商家类型：0-实体店（线下实体门店）、1-中间商（线上进货零售）、2-供货商（线上供应货物）", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    private Integer type;

    @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "羡民演示店")
    private String name = "未知";

    @Schema(description = "徽标", requiredMode = Schema.RequiredMode.REQUIRED)
    private String logo;

    @Schema(description = "门头照", example = "https://www.example.com/banner.png")
    private String banner;

    @Schema(description = "状态：0-未开通、1-营业中、2-停业中", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer status;

    @Schema(description = "开关：0-未封禁、1-已封禁", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    private Integer enabled;

    @Schema(description = "店铺种类（即主营类目）", example = "food")
    private String categoryCode;
    @Schema(description = "店铺种类（即主营类目）", example = "餐饮美食")
    private String categoryName;

    public void assignCategory(EnumCodable codable) {
        if (codable != null) {
            setCategoryCode(codable.getCode());
            setCategoryName(codable.getName());
        } else {
            setCategoryCode(ShopCategoryCodeEnum.UNDEFINED.getCode());
            setCategoryName("");
        }
    }

    @Schema(description = "负责人姓名。小微商户为个人姓名，个体户和企业为法人姓名", example = "李玉江")
    private String contactName;

    @Schema(description = "负责人电话", example = "15285116029")
    private String contactMobile;

    @Schema(description = "身份证号码。小微商户为个人身份证号码，个体户和企业为法人身份证号码")
    @JsonIgnore // 不返回给前端，仅供后端模块内部使用，会用来进行认证对比
    private String ownerEidNo;

    @Schema(description = "身份证正面图片地址", example = "https://example.local/xx.jpg")
    @JsonIgnore // 不返回给前端，仅供后端模块内部使用，会用来进行认证对比
    private String ownerEidFrontUrl;

    @Schema(description = "身份证反面图片地址", example = "https://example.local/xx.jpg")
    @JsonIgnore // 不返回给前端，仅供后端模块内部使用，会用来进行认证对比
    private String ownerEidBackUrl;

    @Schema(description = "简介")
    private String introduction;

    @Schema(description = "营业开始时间")
    private LocalTime openingTime;

    @Schema(description = "营业结束时间")
    private LocalTime closingTime;

    @SuppressWarnings("unused")
    @Schema(description = "营业时间")
    public String getBusinessHours() {
        return formatBusinessHours(getOpeningTime(), getClosingTime());
    }

    @Schema(description = "平台服务费百分比，范围为 0-100 之间的数字")
    @JsonIgnore // 不返回给前端，仅供后端模块内部使用
    private Double serviceFeePercent;

    @Nonnull
    @JsonIgnore // 不返回给前端，仅供后端模块内部使用
    public Double getServiceFeePercent() {
        if (serviceFeePercent == null || serviceFeePercent < 0) {
            serviceFeePercent = 0D;
        }
        return serviceFeePercent;
    }

    @Schema(description = "顾客抵扣百分比，范围为 0-100 之间的数字")
    @JsonIgnore // 不返回给前端，仅供后端模块内部使用
    private Double discountPercent;

    @Nonnull
    @JsonIgnore // 不返回给前端，仅供后端模块内部使用
    public Double getDiscountPercent() {
        if (discountPercent == null || discountPercent < 0) {
            discountPercent = 0D;
        }
        return discountPercent;
    }

    @Schema(description = "下单范围半径，0 为不限制，单位：米", example = "5000")
    private Integer o2oRadius;

    @Schema(description = "标签集", example = "旅馆")
    private List<String> labels;

    @SuppressWarnings("unused")
    public List<String> getLabels() {
        if (labels == null) {
            labels = Collections.emptyList();
        }
        return labels;
    }

    @Schema(description = "评分", example = "4.9")
    private double score;

    public double getScore() {
        if (score <= 0d) {
            score = 5.0;
        }
        return score;
    }

    @Schema(description = "评分星级", example = "5")
    private Integer scoreLevel;

    @SuppressWarnings("unused")
    public Integer getScoreLevel() {
        if (scoreLevel == null) {
            return ScoreLevelEnum.fromScore(getScore()).getValue();
        }
        return scoreLevel;
    }

    @Schema(description = "产品数", example = "0")
    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    private Integer productCount;

    @Schema(description = "评论数", example = "0")
    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    private Integer commentCount;

}
